/**
 * @class   wosh::interfaces::devices::Modem
 * @brief   Standard interface of generic modem (device).
 *
 * @see     wosh::interfaces::devices::ModemDsl
 * @ingroup Interfaces
 ****************************************************************************
 * @version 0.8.499 $Id: Modem.h 2882 2010-08-09 11:08:30Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Interfaces_Devices_Modem_H__
 #define __WOSH_Interfaces_Devices_Modem_H__

 #include <interfaces/Device.h>


namespace wosh {
 namespace interfaces {
  namespace devices {

 #define _Modem_KEY_ConnectionStatus		"ConnectionStatus"
 #define _Modem_KEY_IpAddress				"IpAddress"

 #define _Modem_KEY_statsPacketsTx			"statsPacketsTx"
 #define _Modem_KEY_statsPacketsRx			"statsPacketsRx"
 #define _Modem_KEY_statsBytesTx			"statsBytesTx"
 #define _Modem_KEY_statsBytesRx			"statsBytesRx"
 #define _Modem_KEY_statsErrorsTx			"statsErrorsTx"
 #define _Modem_KEY_statsErrorsRx			"statsErrorsRx"

 #define _Modem_KEY_ConnectedTime			"ConnectedTime"
 #define _Modem_KEY_statsSpeed				"statsSpeed"
 #define _Modem_KEY_statsSpeedTop			"statsSpeedTop"

 #define _Modem_METHOD_connect				"connect"
 #define _Modem_METHOD_disconnect			"disconnect"
 #define _Modem_METHOD_reconnect			"reconnect"
 #define _Modem_METHOD_checkConnected		"checkConnected"
 #define _Modem_METHOD_resetStats			"resetStats"


class Modem : public virtual Device
 {
	WOSH_BUNDLE_INTERFACE("wosh::interfaces::devices::Modem")

	public:
		///  states of the connection
		enum CONNECTION_STATE {
			STATE_UNKNOWN		= 0x00,	///< state is unknown, (default during initialization)
			STATE_CONNECTED		= 0x01,	///< connection is opened
			STATE_DISCONNECTED	= 0x02,	///< connection is closed
			STATE_STALLED		= 0x04	///< connection is not working properly, sw/hw failure
		 };

	public:
		/**
		 * @brief  Evaluate if the connection state is \c CONNECTED. Provided for convenience.
		 * @return true if the state is wosh::interfaces::devices::Modem::CONNECTED; false else
		 */
		inline bool isModemConnected() const	{ return (getConnectionState() == STATE_CONNECTED); }
		/**
		 * @brief  Evaluate if the connection state is \c DISCONNECTED. Provided for convenience.
		 * @return true if the state is wosh::interfaces::devices::Modem::DISCONNECTED; false else
		 */
		inline bool isModemDisconnected() const	{ return (getConnectionState() == STATE_DISCONNECTED); }
		/**
		 * @brief  Evaluate if the connection state is \c STALLED. Provided for convenience.
		 * @return true if the state is wosh::interfaces::devices::Modem::STALLED; false else
		 */
		inline bool isModemStalled() const		{ return (getConnectionState() == STATE_STALLED); }
		/**
		 * @brief   Get connection state as a literal string
		 * @return C string represeting the current state
		 * @see    Modem::getConnectionStateAsString
		 */
		inline const char* getConnectionStateAsString() const { return getConnectionStateAsString(getConnectionState()); }

/** @name Modem Interface
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief  Evaluate connection's state. Implemented by inherited classes.
		 * @return the current state of the connection
		 */
		virtual CONNECTION_STATE getConnectionState() const = 0;

		/**
		 * @brief   Connect modem. The procedure and may be different for each implementation.
		 * @param params Connection parameters (when required)
		 * @return  WRET_NOT_IMPLEMENTED or WRET_ERR_ILLEGAL_USE if feature is not available;
		 *          WRET_ERR_INTERNAL if something went wrong; WRET_OK if modem has been disconnected.
		 */
		virtual WRESULT connect( std::string params = "" ) = 0;
		/**
		 * @brief   Disconnect modem. Goal is to stop any I/O. The procedure and may be different for each implementation.
		 * @return  WRET_NOT_IMPLEMENTED or WRET_ERR_ILLEGAL_USE if feature is not available;
		 *          WRET_ERR_INTERNAL if something went wrong; WRET_OK if modem has been disconnected.
		 */
		virtual WRESULT disconnect() = 0;
		/**
		 * @brief   Reconnect modem (restore previous connection). 
		 * @return  WRET_NOT_IMPLEMENTED or WRET_ERR_ILLEGAL_USE if feature is not available;
		 *          WRET_ERR_INTERNAL if something went wrong; WRET_OK if modem has been disconnected.
		 */
		virtual WRESULT reconnect() = 0;
		/**
		 * @brief   Force connection check.
		 * @return  WRET_NOT_IMPLEMENTED or WRET_ERR_ILLEGAL_USE if feature is not available;
		 *          WRET_ERR_INTERNAL if something went wrong; WRET_OK if check was successful.
		 */
		virtual WRESULT checkConnected() = 0;
		/**
		 * @brief   Force stats reset.
		 * @return  WRET_NOT_IMPLEMENTED or WRET_ERR_ILLEGAL_USE if feature is not available;
		 *          WRET_ERR_INTERNAL if something went wrong; WRET_OK if check was successful.
		 */
		virtual WRESULT resetStats() = 0;

//@}
	public:
		/**
		 * @brief   Get connection state as a literal string
		 * @param state [in] connection state to be retrieved as literal string
		 * @return  passed connection state as literal string
		 */
		static const char* getConnectionStateAsString( CONNECTION_STATE state ) {
			switch(state) {
				case STATE_CONNECTED:		return "CONNECTED";
				case STATE_DISCONNECTED:	return "DISCONNECTED";
				case STATE_STALLED:			return "STALLED";
				case STATE_UNKNOWN:
				default:					return "UNKNOWN";
			 }
		 }
		/**
		 * @brief   Get connection state from a literal string
		 * @param state [in] connection state as literal string
		 * @return  connection state (as enum) associated with the argument 
		 */
		static CONNECTION_STATE getConnectionStateFromString( const char* state ) {
			if ( state == NULL )							return STATE_UNKNOWN;
			else if ( strcmp(state, "CONNECTED") == 0 )		return STATE_CONNECTED;
			else if ( strcmp(state, "DISCONNECTED") == 0 )	return STATE_DISCONNECTED;
			else if ( strcmp(state, "STALLED") == 0 )		return STATE_STALLED;
			return STATE_UNKNOWN;
		 }

	public:
		virtual ~Modem() { }

}; // class def

  }; // namespace devices
 }; // namespace interfaces
}; // namespace wosh

#endif //__WOSH_Interfaces_Devices_Modem_H__
